package com.leoman.statData.controller;

import com.leoman.category.entity.Category;
import com.leoman.category.service.CategoryService;
import com.leoman.common.controller.common.GenericEntityController;
import com.leoman.common.factory.DataTableFactory;
import com.leoman.exception.GeneralExceptionHandler;
import com.leoman.post.entity.PostTypeData;
import com.leoman.resources.entity.ResourceData;
import com.leoman.resources.entity.ResourceDetailData;
import com.leoman.statData.service.StatDataService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.WebUtil;
import com.leoman.welfare.entity.WelfareData;
import com.leoman.work.entity.*;
import com.leoman.work.service.WorkComicService;
import com.leoman.work.service.WorkNovelService;
import com.leoman.work.service.WorkService;
import com.leoman.work.service.WorkVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 数据统计controller
 * Created by 涂奕恒 on 2016/12/15 0022.
 */
@Controller
@RequestMapping(value = "admin/statData")
public class StatDataController {

    @Autowired
    private StatDataService statDataService;

    @Autowired
    private WorkService workService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private WorkVideoService workVideoService;

    @Autowired
    private WorkNovelService workNovelService;

    @Autowired
    private WorkComicService workComicService;

    /**
     * 作品统计列表页面
     */
    @RequestMapping(value = "/index")
    public String workIndex(Integer type, ModelMap model) {
        model.addAttribute("createDate", DateUtils.getTodayDate());

        String url;
        switch (type) {
            case 4:
                // 视频
                url = "work/videoDataList";
                break;
            case 5:
                // 小说
                url = "work/novelDataList";
                break;
            case 6:
                // 漫画
                url = "work/comicDataList";
                break;
            default:
                // 视频
                url = "work/videoDataList";
                break;
        }

        return url;
    }

    /**
     * 作品分集统计列表页面
     */
    @RequestMapping(value = "/work/detail/index")
    public String workDetailIndex(ModelMap model, Long workId) {
        Work work = workService.findOneById(workId);
        model.addAttribute("work", work);
        model.addAttribute("createDate", DateUtils.getTodayDate());

        String url = "";
        switch (work.getCategory().getType()) {
            case 4:
                // 视频
                List<WorkVideo> workVideoList = workVideoService.findByWorkId(workId);
                model.addAttribute("updateCount", workVideoList.size());
                url = "work/videoDataDetailList";
                break;
            case 5:
                // 小说
                List<WorkNovel> workNovelList = workNovelService.findByWorkId(workId);
                model.addAttribute("updateCount", workNovelList.size());
                url = "work/novelDataDetailList";
                break;
            case 6:
                // 漫画
                List<WorkComic> workComicList = workComicService.findByWorkId(workId);
                model.addAttribute("updateCount", workComicList.size());
                url = "work/comicDataDetailList";
                break;
        }
        return url;
    }

    /**
     * 作品分集统计列表页面
     */
    @RequestMapping(value = "/work/detail/indexPlus")
    public String workDetailIndexPlus(ModelMap model, Long workId, Long detailId) {
        Work work = workService.findOneById(workId);
        model.addAttribute("work", work);
        model.addAttribute("detailId", detailId);
        model.addAttribute("createDate", DateUtils.getTodayDate());

        String url = "";
        switch (work.getCategory().getType()) {
            case 4:
                // 视频
                WorkVideo workVideo = workVideoService.findOneById(detailId);
                model.addAttribute("workVideo", workVideo);
                url = "work/videoDataDetail";
                break;
            case 5:
                // 小说
                WorkNovel workNovel = workNovelService.findOneById(detailId);
                model.addAttribute("workNovel", workNovel);
                url = "work/novelDataDetail";
                break;
            case 6:
                // 漫画
                WorkComic workComic = workComicService.findOneById(detailId);
                model.addAttribute("workComic", workComic);
                url = "work/comicDataDetail";
                break;
        }
        return url;
    }

    /**
     * 帖子分类统计列表页面
     */
    @RequestMapping(value = "/post/index")
    public String postIndex() {
        return "category/statDataList";
    }

    /**
     * 帖子分类统计详情列表页面
     */
    @RequestMapping(value = "/post/detail")
    public String postDetail(ModelMap model, Long categoryId) {
        Category category = categoryService.findOneById(categoryId);
        model.addAttribute("createDate", DateUtils.getTodayDate());
        model.addAttribute("category", category);
        return "category/postDataDetailList";
    }

    /**
     * 作品统计查询列表
     */
    @RequestMapping(value = "/work/list")
    public void workList(HttpServletResponse response,
                         Integer type,
                         String name,
                         Integer draw,
                         Integer start,
                         Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);
            Page<WorkData> page = statDataService.pageWorkList(type, name, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }

    /**
     * 作品分集统计查询列表
     */
    @RequestMapping(value = "/work/detail/list")
    public void workDetailList(HttpServletResponse response,
                               Long workId,
                               Integer type,
                               Integer draw,
                               Integer start,
                               Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);
            Page<WorkData> page = statDataService.pageWorkDetailList(workId, type, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }

    /**
     * 作品分集统计查询列表
     */
    @RequestMapping(value = "/work/detail/listPlus")
    public void workDetailListPlus(HttpServletResponse response,
                                   Long workId,
                                   Long detailId,
                                   String startDate,
                                   String endDate,
                                   Integer draw,
                                   Integer start,
                                   Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);
            Page<WorkData> page = statDataService.pageWorkDetailListPlus(workId, detailId, startDate, endDate, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }

    /**
     * 帖子统计查询列表
     */
    @RequestMapping(value = "/post/list")
    public void postList(HttpServletResponse response,
                         String typeName,
                         Integer draw,
                         Integer start,
                         Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);

            Page<PostTypeData> page = statDataService.pageByParams(typeName, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }

    /**
     * 帖子统计查询列表
     */
    @RequestMapping(value = "/post/detail/list")
    public void postDetailList(HttpServletResponse response,
                               Long categoryId,
                               String startDate,
                               String endDate,
                               Integer draw,
                               Integer start,
                               Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);
            Page<PostTypeData> page = statDataService.pagePostList(categoryId, startDate, endDate, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }

    /**
     * 资源统计列表页面
     */
    @RequestMapping(value = "/resource/index")
    public String resourceIndex(Integer type, ModelMap model) {
        model.addAttribute("createDate", DateUtils.getTodayDate());
        String url = "";
        switch (type) {
            case 1:
                // 音乐
                url = "resource/musicDataList";
                break;
            case 2:
                // 剧照
                url = "resource/stillDataList";
                break;
            case 3:
                // 游戏
                url = "resource/gameDataList";
                break;
            case 4:
                // 福利社
                url = "resource/welfareDataList";
                break;
        }

        return url;
    }

    /**
     * 资源详情统计列表页面
     */
    @RequestMapping(value = "/resource/detail/index")
    public String resourceDetailIndex(ModelMap model, Long id, Integer type) {
        model.addAttribute("sourceId", id);
        model.addAttribute("createDate", DateUtils.getTodayDate());

        String url = "";
        switch (type) {
            case 1:
                // 音乐
                url = "resource/musicDataDetailList";
                break;
            case 2:
                // 剧照
                url = "resource/stillDataDetailList";
                break;
            case 3:
                // 游戏
                url = "resource/gameDataDetailList";
                break;
            case 4:
                // 福利社
                url = "resource/welfareDataDetailList";
                break;
        }
        return url;
    }

    /**
     * 资源统计查询列表
     */
    @RequestMapping(value = "/resource/list")
    public void resourceList(HttpServletResponse response,
                             Integer type,
                             Long resourceId,
                             String resourceName,
                             String startDate,
                             String endDate,
                             Integer draw,
                             Integer start,
                             Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);
            Page<ResourceData> page = statDataService.pageResourceList(type, resourceId, resourceName, startDate, endDate, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }

    /**
     * 游戏统计详情查询列表
     */
    @RequestMapping(value = "/resource/gameList")
    public void resourceGameList(HttpServletResponse response,
                                 Integer type,
                                 Long resourceId,
                                 Integer draw,
                                 Integer start,
                                 Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);
            Page<ResourceData> page = statDataService.pageResourceList(type, resourceId, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }

    /**
     * 游戏统计详情列表页面
     */
    @RequestMapping(value = "/game/detail")
    public String gameDetail(ModelMap model, Long resourceId) {
        model.addAttribute("resourceId", resourceId);
        return "resource/gameDetailDataList";
    }

    /**
     * 游戏统计详情列表页面
     */
    @RequestMapping(value = "/game/detailPlus")
    public String gameDetailPlus(ModelMap model, Long resourceId) {
        model.addAttribute("gameResource", statDataService.findGameByResourceId(resourceId));
        model.addAttribute("createDate", DateUtils.getTodayDate());
        return "resource/gameDetailList";
    }

    /**
     * 福利社统计详情列表页面
     */
    @RequestMapping(value = "/welfare/detail")
    public String welfareDetail(ModelMap model, Long welfareId) {
        model.addAttribute("welfare", statDataService.findWelfareByWelfareId(welfareId));
        model.addAttribute("createDate", DateUtils.getTodayDate());
        return "resource/welfareDetailList";
    }

    /**
     * 资源统计查询列表
     */
    @RequestMapping(value = "/game/list")
    public void gameList(HttpServletResponse response,
                         Long resourceId,
                         String nickName,
                         Integer status,
                         Integer draw,
                         Integer start,
                         Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);
            Page<ResourceDetailData> page = statDataService.pageGameList(resourceId, nickName, status, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }

    /**
     * 资源统计查询列表
     */
    @RequestMapping(value = "/welfare/list")
    public void welfareList(HttpServletResponse response,
                            Long welfareId,
                            String resourceName,
                            String startDate,
                            String endDate,
                            Integer draw,
                            Integer start,
                            Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);
            Page<WelfareData> page = statDataService.pageWelfareList(welfareId, resourceName, startDate, endDate, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }

    /**
     * 资源统计列表页面
     */
    @RequestMapping(value = "/resource/base")
    public String resourceBase(ModelMap model) {
        model.addAttribute("createDate", DateUtils.getTodayDate());
        return "resource/baseDataList";
    }

    /**
     * 资源统计查询列表
     */
    @RequestMapping(value = "/stat/list")
    public void statList(HttpServletResponse response,
                         Integer type,
                         Integer draw,
                         Integer start,
                         Integer length) {
        try {
            int pageNum = GenericEntityController.getPageNum(start, length);
            Page<ResourceData> page = statDataService.pageResourceList(type, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, GenericEntityController.emptyData);
        }
    }
}
